<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="tpId"/>
      <el-table-column label="输出结果" align="center" prop="tpResult">
        <template slot-scope="scope">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item
              :title="(function() { return scope.row.tpResult ? scope.row.tpResult.slice(0, 25) : '默认标题'; })()"
              :name="`unique-name-${scope.row.id}`">
              <div>{{ scope.row.tpResult }}</div>
            </el-collapse-item>
          </el-collapse>
        </template>

      </el-table-column>
      <el-table-column label="图表1" align="center" prop="tpResult">
        <template slot-scope="scope">
          <el-badge align="center">
            <e-charts class="chart1" :option="option1(scope.row.tpResult)"/>
          </el-badge>
          <!--          <h1>{{ data }}</h1>-->
        </template>
      </el-table-column>
      <el-table-column label="图表2" align="center" prop="tpResult">
        <template slot-scope="scope">
          <el-badge align="center">
            <e-charts class="chart2" :option="option2(scope.row.tpResult)"/>
          </el-badge>
          <!--          <h1>{{ data }}</h1>-->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['wordcloud:distr:edit']"-->
          <!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wordcloud:distr:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改展示词云图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMain, getMain, delMain, addMain, updateMain} from "@/api/wordcloud/main";
import * as https from "https";
import echarts from 'echarts';
import Data from "@/views/system/dict/data.vue";

export default {
  name: "Main",
  components: {Data},
  computed: {
    https() {
      return https
    },
  },
  data() {
    return {
      //data: this.processedData(),
      data: [],
      rawData: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWordcloudSlave: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 展示词云图表格数据
      mainList: [],
      // 词云图从表表格数据
      wordcloudSlaveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tpContext: null,
        tpResult: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.processedData();
  },
  methods: {
    option1(rawData) {
      this.processedData(rawData)
      return {
        xAxis : [
          {
            type : 'category',
            data : [this.data[0].name,this.data[1].name,this.data[2].name,this.data[3].name,this.data[4].name,this.data[5].name]
          }
        ],
        yAxis : [
          {
            type : 'value'
          }
        ],
        series : [
          {
            name:'蒸发量',
            type:'bar',
            data:[this.data[0].value,this.data[1].value,this.data[2].value,this.data[3].value,this.data[4].value,this.data[5].value],
          },
        ]
        // xAxis: [
        //   {
        //     type: 'word',
        //     data: [this.data[0].name,this.data[1].name,this.data[2].name,this.data[3].name,this.data[4].name,this.data[5].name]
        //     //data:this.data.map(d => d.name)
        //   }
        // ],
        // yAxis: [
        //   {
        //     type: 'frequecy'
        //   }
        // ],
        // series: [
        //   {
        //     name: '词频',
        //     type: 'bar',
        //     data: [this.data[0].value,this.data[1].value,this.data[2].value,this.data[3].value,this.data[4].value,this.data[5].value],
        //     //data:this.data.map(d => d.value)
        //   }
        // ]
      }
    },
    option2(rawData) {
      this.processedData(rawData)
      return {
        calculable: true,
        series: [
          {
            name: '面积模式',
            type: 'pie',
            center: ['35%', 200],
            radius: [20, 80],
            roseType: 'area',
            max: 30,
            sort: 'ascending',
            data: [
              {value: this.data[0].value, name: this.data[0].name},
              {value: this.data[1].value, name: this.data[1].name},
              {value: this.data[2].value, name: this.data[2].name},
              {value: this.data[3].value, name: this.data[3].name},
              {value: this.data[4].value, name: this.data[4].name},
              {value: this.data[5].value, name: this.data[5].name},
            ]
          }
        ]
      }
    },
    processedData(rawData) {
      // 将 rawData 转换为所需格式的逻辑,获取前十个数据
      let pairs = rawData.split(',').slice(0, 10);
      let data = []
      pairs.forEach(pair => {
        pair = pair.trim();
        if (pair) {
          let [word, value] = pair.split(':');
          data.push({
            name: word, // 使用 'name' 作为键名，这是 ECharts 等图表库常用的键名
            value: parseInt(value, 10) // 将值转换为整数，指定基数为 10
          });
        }
      });
      this.data = data


    },
    /** 查询展示词云图列表 */
    getList() {
      this.loading = true;
      listMain(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tpId: null,
        tpContext: null,
        tpResult: null,
        type: null
      };
      this.wordcloudSlaveList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.tpId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加展示词云图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tpId = row.tpId || this.ids
      getMain(tpId).then(response => {
        this.form = response.data;
        this.wordcloudSlaveList = response.data.wordcloudSlaveList;
        this.open = true;
        this.title = "修改展示词云图";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wordcloudSlaveList = this.wordcloudSlaveList;
          if (this.form.tpId != null) {
            updateMain(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMain(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tpIds = row.tpId || this.ids;
      this.$modal.confirm('是否确认删除展示词云图编号为"' + tpIds + '"的数据项？').then(function () {
        return delMain(tpIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 词云图从表序号 */
    rowWordcloudSlaveIndex({row, rowIndex}) {
      row.index = rowIndex + 1;
    },
    /** 词云图从表添加按钮操作 */
    handleAddWordcloudSlave() {
      let obj = {};
      obj.tcPic = "";
      this.wordcloudSlaveList.push(obj);
    },
    /** 词云图从表删除按钮操作 */
    handleDeleteWordcloudSlave() {
      if (this.checkedWordcloudSlave.length == 0) {
        this.$modal.msgError("请先选择要删除的词云图从表数据");
      } else {
        const wordcloudSlaveList = this.wordcloudSlaveList;
        const checkedWordcloudSlave = this.checkedWordcloudSlave;
        this.wordcloudSlaveList = wordcloudSlaveList.filter(function (item) {
          return checkedWordcloudSlave.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWordcloudSlaveSelectionChange(selection) {
      this.checkedWordcloudSlave = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wordcloud/main/export', {
        ...this.queryParams
      }, `main_${new Date().getTime()}.xlsx`)
    },
    mounted() {
    },
  }
};
</script>

<style>
.chart1 {
  height: 350px;
  width: 350px;
}

.chart2 {
  height: 400px;
  width: 400px;
}
</style>>

